#!/bin/sh

############################################################################
#                                                                          #
#       encryptit: Encrypts the file with the default encryption key       #
#                                                                          #
############################################################################

#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#  Copyright (c) 2011 Sercan Arslan <arslanserc@gmail.com>
#

. /etc/init.d/tc-functions

TET_ROOT="$(dirname $0)/.."

if [ -f "$TET_ROOT/share/tet/functions.sh" ]; then
     . "$TET_ROOT/share/tet/functions.sh"
else
     echo "${RED}$(basename $0):${BLUE} tc-ext-tools functions file not found!${NORMAL}"
     exit 1
fi

if [ -n "$HAVE_TET_PACKAGE" -a ! -f "$PACKAGE_PACKAGE_STAMP" ]; then
     echo "${ERROR_COLOR}You must package it first!${NORMAL}"
     exit 1
fi

this_main() {

   echo "---------------------------------------------------------------"
   echo ""
   echo "Encrypting the package $PACKAGE ..."
   echo ""
   echo "---------------------------------------------------------------"

   echo -e "Checking if the package $PACKAGE tarball encrypted exists ... \c"
   if [ -f "$PACKAGE_PACKAGE_ENCRYPTED" ]; then
        echo "yes"
        echo -e "Removing the package $PACKAGE tarball encrypted ... \c"
        sudo rm -f "$PACKAGE_PACKAGE_ENCRYPTED"
        echo "done"
   else
        echo "no"
   fi

   echo -e "Checking if the package $PACKAGE tarball exists ... \c"
   if [ -f "$PACKAGE_PACKAGE" ]; then
        echo "yes"
   else
        echo "no"
        echo "Error! the package tarball does not exist ..."
        return 1
   fi

   echo "---------------------------------------------------------------"
   echo "Using bcrypt to encrypt the package $PACKAGE tarball with the encryption key: $PACKAGE_ENCRYPTION_KEY"
   echo "---------------------------------------------------------------"

   bcrypt "$PACKAGE_PACKAGE" << EOS
${PACKAGE_ENCRYPTION_KEY}
${PACKAGE_ENCRYPTION_KEY}
EOS
   if [ "$?" -gt 0 ]; then
        echo "---------------------------------------------------------------"
        echo ""
        echo "Encrypting the package $PACKAGE failed."
        echo ""
        echo "---------------------------------------------------------------"
        return 1
   else
        echo "---------------------------------------------------------------"
        echo ""
        echo "Encrypted the package $PACKAGE successfully."
        echo ""
        echo "---------------------------------------------------------------"
   fi

   return 0

}

[ -f "$PACKAGE_ENCRYPT_STAMP" ] && sudo rm -f "$PACKAGE_ENCRYPT_STAMP"

echo -e "${MESSAGE_COLOR}Encrypting the package ${MESSAGE_COLOR_H}$PACKAGE${MESSAGE_COLOR} with the encryption key: ${MESSAGE_COLOR_H}$PACKAGE_ENCRYPTION_KEY${MESSAGE_COLOR} ... ${NORMAL}\c"
this_main > "$PACKAGE_MESSAGE_LOG" 2>&1
if [ "$?" -gt 0 ]; then
     echo "${ERROR_COLOR}failed!${NORMAL}"
     echo "${WARNING_COLOR}For details: ${WARNING_COLOR_H}buildit --print message${NORMAL}"
     exit 1
fi
echo "${MESSAGE_COLOR_H}successful!${NORMAL}"

touch "$PACKAGE_ENCRYPT_STAMP"

exit 0

